<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctxPath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <title>数据列表</title>
  <link rel="stylesheet" href="${ctxPath}/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="${ctxPath}/layuiadmin/style/admin.css" media="all">
  <link rel="stylesheet" href="${ctxPath}/layuiadmin/modules/ext/soulTable.css" media="all">
<%--body渲染--%>
<style>
  body{margin: 10px;}
</style>
</head>
<body>
<%--数据表格--%>
<table class="layui-hide" id="demo" lay-filter="demo"></table>
 <%--表内工具栏--%>
<script type="text/html" id="toolBar">
  <a class="layui-btn layui-btn-normal layui-btn-xs layui-btn-radius" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<%--头部工具栏--%>
<script type="text/html" id="headToolBar">
<div class="layui-inline">
  <div class="layui-btn-group">
    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="add">
      <i class="layui-icon">&#xe654;</i>
    </button>
    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="update">
      <i class="layui-icon">&#xe642;</i>
    </button>
    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="delete">
      <i class="layui-icon">&#xe640;</i>
    </button>
    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="reload">
      <i class="layui-icon">&#xe669;</i>
    </button>
    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="clear">
      <i class="layui-icon">&#xe60e;</i>
    </button>
    <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="switch">
        <i class="layui-icon">&#xe630;</i>
    </button>
  </div>
</div>
<div class="layui-inline" style="margin-left: 5%">
  机构名称
<input class="layui-input layui-input-inline layui-input-search" autocomplete="off" placeholder="请输入" id="searchName" style="width: 120px;height:30px"/>
</div>

<div class="layui-btn-group">
<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="search" data-type="reload">
  <i class="layui-icon">&#xe615;</i>
</button>
</div>

</script>
<script src="${ctxPath}/layuiadmin/layui/layui.js"></script>
<script src="${ctxPath}/js/jquery-1.11.1.min.js"></script>
<script src="${ctxPath}/js/layui-customtips.js"></script>
<!-- layui代码 -->
<script>
//ran防止懒惰，r随机动画
{var ran = Math.random();
var r=Math.floor(Math.random()*6);
var switchflag=false;}
//自定义模块
layui.config({
  base: '${ctxPath}layuiadmin/modules/ext/',   // 第三方模块所在目录
  version: 'v1.5.19' // 插件版本号
})
.extend({
    soulTable: 'soulTable',
    tableChild: 'tableChild',
    tableMerge: 'tableMerge',
    tableFilter: 'tableFilter',
    excel: 'excel',
    treeSelect:'treeSelect',
    treetable:'treetable'
  });
//使用模块
layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel',
          'upload', 'element', 'slider','soulTable','treeSelect',
          'treetable']
,function(){
//声明模块变量
{var laydate = layui.laydate
,laypage = layui.laypage
,layer = layui.layer
,table = layui.table
,carousel = layui.carousel
,upload = layui.upload
,element = layui.element
,slider = layui.slider
,admin = layui.admin
,form = layui.form
,soulTable=layui.soulTable
,treeSelect=layui.treeSelect
,$ = layui.$;}
//渲染表格方法
var renderSoulTable =function(){
table.render({
  elem: '#demo'
  ,id:'demo'
  ,height: 'full-100'
  ,url: '${ctxPath}/getDataBySysDepartment?ran='+ ran
  ,title: '部门表'
  ,page: true
  ,toolbar: '#headToolBar'
  ,totalRow: false //{ps} 开启合计行
  ,limit:20
  ,limits:[10,20,30,50,100,200,500,1000,2000]
  ,cellMinWidth:80
  ,cols: [[
    {type: 'checkbox',fixed:'left'},
    {field: 'id', title: 'ID', width:100, sort: true},
    {field: 'parentdepartmentid', title: '父部门',sort:true,hide:true, width:100,filter:true},
    {field: 'name', title: '机构名',sort:true, width:180,edit:'text',filter:true},
    {field: 'sort', title: '排列', width:80,sort:true,hide:true,edit:'text',filter:true},
    {field: 'description', title: '描述', edit:'text',filter:true},
    {field: 'type', title: '类型', width:120,sort:true,edit:'text',filter:true},
    {field: 'area', title: '地点',sort:true, width:100,edit:'text',filter:true},
    {fixed: 'right',align:'center',title:'***',templet: '<div><i class="layui-icon layui-icon-snowflake layui-drag" /></div>',minWidth:50,maxWidth:50,width:50},
    {fixed: 'right', title:'操作', align:'center', toolbar: '#toolBar',minWidth:150}
  ]]
  ,initSort:{
    field:'sort',
    type:'asc',
  }
  ,filter: {
    items:['column','data','condition','editCondition','excel','clearCache'],
    cache: true
  }
  ,overflow: {
    type: 'tips'
    ,hoverTime: 300
    ,color: 'black'
    ,bgColor: 'white'
    ,minWidth: 100
    ,maxWidth: 500
  }
  ,contextmenu: {
    header: [
      {
        name: '复制',
        icon: 'layui-icon layui-icon-template',
        click: function(obj) {
          soulTable.copy(obj.text)
          layer.msg('复制成功', {icon: 1, time: 1000})
        }
      },
      {
        name: '导出excel',
        click: function() {
          soulTable.export(this.id)
        }
      },
      {
        name: '重载表格',
        icon: 'layui-icon layui-icon-refresh-1',
        click: function() {
          table.reload(this.id);
        }
      },
      {
        name: '清除缓存',
        icon: 'layui-icon layui-icon-log',
        click: function() {
          layer.confirm('清除缓存会使显示风格还原，确认？',{icon:3,title:'提示信息'}, function(index){
            soulTable.clearCache("demo");
            layer.msg('已清缓存', {icon: 1, time: 1000});
          });
        }
      },
      {
        name: '字体颜色',
        icon: 'layui-icon layui-icon-theme',
        children: [
          {
            name: '大红色',
            click: function(obj) {
              obj.elem.css('color', '#aa0200')
            }
          },
          {
            name: '海蓝色',
            click: function(obj) {
              obj.elem.css('color', '#1000aa')
            }
          },
          {
            name: '柑橘色',
            click: function(obj) {
              obj.elem.css('color', '#aa6d00')
            }
          },
          {
            name: '翠绿色',
            click: function(obj) {
              obj.elem.css('color', '#00aa22')
            }
          }
          ,
          {
            name: '还原字体',
            click: function(obj) {
              obj.elem.css('color', '')
            }
          }
        ]
      }
      ,
      {
        name: '背景色',
        icon: 'layui-icon layui-icon-theme',
        children: [
          {
            name: '浅海蓝',
            click: function(obj) {
              obj.elem.css('background', '#00caed')
            }
          },
          {
            name: '鲜花红',
            click: function(obj) {
              obj.elem.css('background', '#ed8670')
            }
          },
          {
            name: '草原绿',
            click: function(obj) {
              obj.elem.css('background', '#99ed48')
            }
          },
          {
            name: '香蕉黄',
            click: function(obj) {
              obj.elem.css('background', '#f7ff50')
            }
          },
          {
            name: '还原背景',
            click: function(obj) {
              obj.elem.css('background', '')
            }
          }
        ]
      }
    ],
    body: [
      {
        name: '复制',
        icon: 'layui-icon layui-icon-template',
        click: function(obj) {
          soulTable.copy(obj.text)
          layer.msg('复制成功', {icon: 1, time: 1000})
        }
      },
      {
        name: '删除当前行数据',
        click: function(obj) {
          layer.confirm('删除\''+obj.row.name+'\'吗',{icon:3,title:'提示信息'}, function(index){
            del( obj.row.id );
          });
        }
      },
      {
        name: '编辑数据',
        click: function(obj) {
          edit(obj.row.id);
        }
      },
      {
        name: '隐藏当前行数据',
        click: function(obj) {
          obj.del();
        }
      },
      {
        name: '字体颜色',
        icon: 'layui-icon layui-icon-theme',
        children: [
          {
            name: '大红色',
            click: function(obj) {
              obj.elem.css('color', '#aa0200')
            }
          },
          {
            name: '海蓝色',
            click: function(obj) {
              obj.elem.css('color', '#1000aa')
            }
          },
          {
            name: '柑橘色',
            click: function(obj) {
              obj.elem.css('color', '#aa6d00')
            }
          },
          {
            name: '翠绿色',
            click: function(obj) {
              obj.elem.css('color', '#00aa22')
            }
          },
          {
            name: '还原字体',
            click: function(obj) {
              obj.elem.css('color', '')
            }
          }
        ]
      },
      {
        name: '单-背景色',
        icon: 'layui-icon layui-icon-theme',
        children: [
          {
            name: '浅海蓝',
            click: function(obj) {
              obj.elem.css('background', '#00caed')
            }
          },
          {
            name: '鲜花红',
            click: function(obj) {
              obj.elem.css('background', '#ed8670')
            }
          },
          {
            name: '草原绿',
            click: function(obj) {
              obj.elem.css('background', '#99ed48')
            }
          },
          {
            name: '香蕉黄',
            click: function(obj) {
              obj.elem.css('background', '#f7ff50')
            }
          },
          {
            name: '还原单背景',
            click: function(obj) {
              obj.elem.css('background', '')
            }
          }
        ]
      },
      {
        name: '行-背景色',
        icon: 'layui-icon layui-icon-theme',
        children: [
          {
            name: '浅海蓝',
            click: function(obj) {
              obj.trElem.css('background', '#00caed')
            }
          },
          {
            name: '鲜花红',
            click: function(obj) {
              obj.trElem.css('background', '#ed8670')
            }
          },
          {
            name: '草原绿',
            click: function(obj) {
              obj.trElem.css('background', '#99ed48')
            }
          },
          {
            name: '香蕉黄',
            click: function(obj) {
              obj.trElem.css('background', '#f7ff50')
            }
          },
          {
            name: '还原行背景',
            click: function(obj) {
              obj.trElem.css('background', '')
            }
          }
        ]
      }
    ]}
  ,soulSort:true
  ,excel:{
    on: true,
    filename: '数据.xlsx',
    head:{
      family: 'Calibri',
      size: 16,
      color: '000000',
      bgColor: 'C7C7C7'
    },
    font: { // 正文样式
      family: 'Calibri',
      size: 12,
      color: '000000',
      bgColor: 'ffffff'
    }
  }
  ,rowDrag: {trigger: '.layui-drag'}
  ,drag: {toolbar: true}
  ,text:'对不起，加载出现异常！请联系小蓝解决！'
  ,done: function(res, curr, count){
    soulTable.render(this);
    if (res.count == 0) {
      $(".layui-none").hide();//隐藏无数据
      $(".layui-table-page").hide();//隐藏分页
    }
  }
});
  }
//执行渲染表格方法
renderSoulTable();
//监听Tab切换
element.on('tab(demo)', function(data){
        layer.tips('切换了 '+ data.index +': '+ this.innerHTML, this, {
          tips: 1
        });
      });
//监听头工具栏事件
table.on('toolbar(demo)', function(obj){
  var checkStatus = table.checkStatus(obj.config.id)
  ,data = checkStatus.data; //获取选中的数据
  switch(obj.event){
    case 'add':
      layer.open({
          type: 2
          ,id:"onlyone"
          ,content: '${ctxPath}/toSysDepartMentAdd'
          ,shade:0
          ,shadeClose: false
          ,moveOut:false
          ,area: ['80%', '80%']
          ,maxmin: true
          ,anim:r
          ,title:'创建'
        });
    break;
    case 'update':
      if(data.length === 0){
          layer.msg('请选择一行',{icon:2,time:2000,offset:'20px'});
      }  else if(data.length > 1){
          layer.msg('只能编辑一行',{icon:2,time:2000,offset:'20px'});
      }  else {
         edit( checkStatus.data[0].id );
      }
    break;
    case 'delete':
      if(data.length === 0){
          layer.msg('请选择一行',{icon:2,time:2000,offset:'20px'});
      } else{
          var arrayName=[];
          for(var i=0;i<data.length;i++){
            arrayName.push(data[i].name);
          }
          layer.confirm('删除'+arrayName.toString()+'吗',{icon:3,title:'提示信息'}, function(index){
            var arrayId=[];
            for(var i=0;i<data.length;i++){
              arrayId.push(data[i].id);
            }
            dels( arrayId );
            });
      }
    break;
    case 'reload':
      $('#searchName').val("");
      layer.msg('已重载', {icon: 1, time: 1000})
      reloadPage();
      break;
    case 'clear':
      layer.confirm('清除缓存会使显示风格还原，确认？',{icon:3,title:'提示信息'}, function(index){
        soulTable.clearCache("demo");
        layer.msg('已清缓存', {icon: 1, time: 1000});
      });
      break;
    case 'search':
      if(switchflag){
        var keyword = $("#searchName").val();
        var searchCount = 0;
        $('#demo').next('.treeTable').find('.layui-table tbody tr td').each(function () {
          $(this).css('background-color', 'transparent');
          var text = $(this).text();
          if (keyword != '' && text.indexOf(keyword) >= 0) {
            $(this).css('background-color', 'rgba(250,230,160,0.5)');
            if (searchCount == 0) {
              layui.treetable.expandAll('#demo');
              $('html,body').stop(true);
              $('html,body').animate({scrollTop: $(this).offset().top - 150}, 500);
            }
            searchCount++;
          }
        });
        if (keyword == '') {
          reloadPage();
        } else if (searchCount == 0) {
          layer.msg("没有匹配结果", {icon: 5, time: 1000});
        }
      }else{reloadPage();}
    break;
    case 'switch':
        if(switchflag){renderSoulTable();switchflag=false;}
        else{renderTreeTable();switchflag=true;}
    break;
  };
});
//监听行工具事件
table.on('tool(demo)', function(obj){
  var data = obj.data
  , layEvent = obj.event;
  if(layEvent === 'del'){
      layer.confirm('删除\''+data.name+'\'吗',{icon:3,title:'提示信息'}, function(index){
      del( data['id'] );
    });
  } else if(layEvent === 'edit'){
      edit( data['id'] );
  }
});
//监听单元格编辑
table.on('edit(demo)', function(obj){
  var value = obj.value //得到修改后的值
  ,_data = obj.data //得到所在行所有键值
  ,field = obj.field; //得到字段
  $.ajax({
              url:'${ctxPath}/findSysDepartMentById', /* 数据接口 */
              type:'post',
              data:{id:_data.id},
              dataType:'json',
              success:function( data ){
                  if( data['code']=='0' ){
                      var _data =data['data'];
                      _data[field]=value;
                      console.log( _data );
                      $.ajax({
                          url:'${ctxPath}/updateBySysDepartMentPrimaryKey',
                          type:'post',
                          data:_data,
                          dataType:'json',
                          success:function( data ){
                              var code = data['code'];
                              layer.closeAll();
                              if(code=='0'){
                                  layer.msg('更新'+ _data['name'] +'成功',{icon:1,time:2000,offset:'20px'});
                              }else{
                                  layer.msg('更新'+ _data['name'] +'失败, 原因: '+ data['msg'],{icon:2,time:2000,offset:'20px'});
                              }
                          }
                      });
                  }else{
                      layer.msg('访问数据错误。');
                  }
              }
          });
});
//监听行双击事件
table.on('rowDouble(demo)', function(obj){
    edit( obj.data.id );
});
//监听弹层提交事件
form.on('submit(layer-submit)', function(data){
  save();
  return false;
});
//验证规则
form.verify({
      sort: [/^[1-9]{1}\d{0,2}$/, '顺序只能数字且三位内']
  });
});
// 定义实现树型结构的表格
var renderTreeTable = function () {
    layui.treetable.render({
      treeColIndex: 1
      ,treeSpid: 0
      ,title: '部门表'
      ,page: true
      ,height: 'full-100'
      ,toolbar: '#headToolBar'
      ,limit:1000
      ,limits:[10,20,50,100,200,500,1000,2000]
      ,treeIdName: 'id'
      ,treePidName: 'parentdepartmentid'
      ,treeDefaultClose: false
      ,treeLinkage: false
      ,elem: '#demo'
      ,url: '${ctxPath}/getDataBySysDepartment?ran='+ ran
      ,page: false
      ,id : "tables"
      ,cols: [[
        {type: 'checkbox',fixed:'left'},
        {field: 'name', title: '机构树', minWidth:300, sort: true},
        {field: 'id', title: 'ID',sort:true, width:120,filter:true},
        {field: 'parentdepartmentid', title: '父部门',sort:true,hide:true, width:100,filter:true},
        {field: 'sort', title: '排列', width:80,sort:true,hide:true,edit:'text',filter:true},
        {field: 'description', title: '描述',edit:'text',filter:true},
        {field: 'type', title: '类型', width:120,sort:true,edit:'text',filter:true},
        {field: 'area', title: '地点',sort:true, width:100,edit:'text',filter:true},
        {fixed: 'right', title:'操作', align:'center', toolbar: '#toolBar',minWidth:150}
      ]]
      ,done: function () {
        layui.layer.closeAll('loading');
      }
    });
  };
</script>

<!-- 我们的业务代码 -->
<script>
//修改
function edit( id ){
	$.ajax(
		{
			url:'${ctxPath}/findSysDepartMentById',
			type:'post',
			data:{id:id},
			dataType:'json',
			success:function( data ){
				if( data['code']=='0' ){
                  createEditLayer( data.data );
				}else{
                  layer.msg('访问数据错误',{icon:2,time:2000,offset:'20px'});
				}
			}
		}
	);
}
//创建修改弹层操作
function createEditLayer( data ){
    //HTMl编辑
    {var HTML="<div class=\"layui-fluid\">"+
	"<div class=\"layui-card\">"+
	"<div class=\"layui-card-header\" >"+
    "<div class=\"layui-input-inline\" lay-value=\""+data['id']+"\">"+data['name']+"</div>"+
    "</div>"+
	"<div class=\"layui-card-body\" style=\"padding: 15px;\">"+
	"<form class=\"layui-form\" action=\"\" lay-filter=\"component-form-group\">"+
		"<div class=\"layui-col-md6 layui-col-sm12\">"+
			"<div class=\"layui-form-item\">"+
			"<label class=\"layui-form-label\">ID</label>"+
			"<div class=\"layui-input-inline\">"+
			"<input value='"+data['id']+"' type=\"text\" id=\"id\" name=\"id\" lay-verify=\"required\" placeholder=\""+data['name']+"的ID\" autocomplete=\"off\" class=\"layui-input\" readonly>"+
			"</div>"+
			"<div class=\"layui-form-mid layui-word-aux\">只读</div>"+
			"</div>"+
		"</div>"+
		"<div class=\"layui-col-md6 layui-col-sm12\">"+
			"<div class=\"layui-form-item\">"+
			"<label class=\"layui-form-label\">父级机构</label>"+
			"<div class=\"layui-input-inline\">"+
            "<input type=\"hidden\" id=\"parentdepartmentid\" name=\"parentdepartmentid\" value='"+data['parentdepartmentid']+"' lay-verify=\"required\" autocomplete=\"off\"/>"+
            "<input type=\"text\" id=\"tree\" lay-filter=\"tree\" class=\"layui-input\" autocomplete=\"off\"/>";
            var treeSelect=layui.treeSelect;
	        treeSelect.render({
                elem: '#tree',
                data: '${ctxPath}/getSysDepartMentTreeDataById?id=0',
                type: 'get',
                placeholder: '请选择部门',
                search: true,
                style: {folder: {enable: false},line: {enable: true}},
                click: function(d){
                    $("#parentdepartmentid").val(d.current.id);
                },
                success: function (d) {
                    if(data['parentdepartmentid']!=0) {
                        treeSelect.checkNode('tree', data['parentdepartmentid']);
                    }
                        var treeObj = treeSelect.zTree('tree');
                        treeSelect.refresh('tree');
                }
            });
            HTML+="</div>"+
			"</div>"+
		"</div>"+
		"<div class=\"layui-col-md6 layui-col-sm12\">"+
			"<div class=\"layui-form-item\">"+
			"<div class=\"layui-inline\">"+
			"<label class=\"layui-form-label\">机构名称</label>"+
			"<div class=\"layui-input-inline\">"+
            "<input type=\"text\" id=\"name\" name=\"name\" value='"+data['name']+"' class=\"layui-input\" lay-verify=\"required\" autocomplete=\"on\"/>"+
			"</div>"+
			"</div>"+
			"</div>"+
		"</div>"+
        "<div class=\"layui-col-md6 layui-col-sm12\">"+
            "<div class=\"layui-form-item\">"+
            "<div class=\"layui-inline\">"+
            "<label class=\"layui-form-label\">描述信息</label>"+
            "<div class=\"layui-input-inline\">"+
            "<input type=\"text\" id=\"description\" name=\"description\" value='"+data['description']+"' class=\"layui-input\" lay-verify=\"required\" autocomplete=\"on\"/>"+
            "</div>"+
            "</div>"+
            "</div>"+
        "</div>"+
        "<div class=\"layui-col-md6 layui-col-sm12\">"+
            "<div class=\"layui-form-item\">"+
            "<div class=\"layui-inline\">"+
            "<label class=\"layui-form-label\">显示顺序</label>"+
            "<div class=\"layui-input-inline\">"+
            "<input type=\"text\" id=\"sort\" name=\"sort\" value='"+data['sort']+"' class=\"layui-input\" lay-verify=\"sort\" autocomplete=\"on\"/>"+
            "</div>"+
            "<div class=\"layui-form-mid layui-word-aux\" custom-tips=\"三位内的正整数\">仅限数字</div>"+
            "</div>"+
            "</div>"+
        "</div>"+
        "<div class=\"layui-col-md6 layui-col-sm12\">"+
            "<div class=\"layui-form-item\">"+
            "<div class=\"layui-inline\">"+
            "<label class=\"layui-form-label\">类型</label>"+
            "<div class=\"layui-input-inline\">"+
            "<input type=\"text\" id=\"type\" name=\"type\" value='"+data['type']+"' class=\"layui-input\" lay-verify=\"required\" autocomplete=\"on\"/>"+
            "</div>"+
            "</div>"+
            "</div>"+
        "</div>"+
        "<div class=\"layui-col-md6 layui-col-sm12\">"+
            "<div class=\"layui-form-item\">"+
            "<div class=\"layui-inline\">"+
            "<label class=\"layui-form-label\">地点</label>"+
            "<div class=\"layui-input-inline\">"+
            "<input type=\"text\" id=\"area\" name=\"area\" value='"+data['area']+"' class=\"layui-input\" lay-verify=\"required\" autocomplete=\"on\"/>"+
            "</div>"+
            "</div>"+
            "</div>"+
        "</div>"+
		"<div class=\"layui-form-item\">"+
		"</div>"+
        "<div class=\"layui-form-item\">" +
            "<div class=\"layui-layer-btn layui-layer-btn-c\">" +
            "<button class=\"layui-btn\" lay-submit=\"\" lay-filter=\"layer-submit\">保存修改</button>" +
            "</div>"+
        "</div>"+
	"</form>"+
	"</div>"+
	"</div>"+
	"</div>";}
	layer.open({
         type: 1
         ,title: '编辑'
         ,closeBtn: false
         ,skin: 'layui-layer-rim'
         ,area: ['80%','80%']
         ,shadeClose: true
         ,id: 'layui-edit-only'
         ,btnAlign: 'c'
         ,moveType: 1
         ,content: HTML
         ,anim:r
         ,success: function( layero ){}
	});
}
//单个删除
function del( _id){
  var table=layui.table;
	$.ajax({
		url:'${ctxPath}/deleteSysDepartMentById',
		type:'post',
		data:{id:_id},
		dataType:'json',
		success:function( data ){
			var code = data['code'];
			layer.closeAll();
			if(code=='0'){
				layer.msg('删除：'+_id+'成功',{icon:1,time:2000,offset:'20px'});
                reloadPage();
			}else{
				layer.msg('删除:'+_id+'失败, 原因:'+ data['msg'],{icon:2,time:2000,offset:'20px'});
			}
		}
	});
}
//批量删除
function dels( _ids){
  var table=layui.table;
  var _id=_ids.toString();
  $.ajax({
    url:'${ctxPath}/deleteSysDepartMentsById',
    type:'post',
    data:{ids:_id},
    dataType:'json',
    success:function( data ){
      var code = data['code'];
      layer.closeAll();
      if(code=='0'){
        layer.msg('删除：'+_id+'成功',{icon:1,time:2000,offset:'20px'});
        reloadPage();
      }else{
        layer.msg('删除:'+_id+'失败, 原因:'+ data['msg'],{icon:2,time:2000,offset:'20px'});
      }
    }
  });
}
//重载本页面
function reloadPage(){
  var demoReload = $('#searchName');
  var table=layui.table;
  var soulTable=layui.soulTable;
  var treeTable=layui.treetable;
  if(switchflag===false){
    table.reload('demo', {
      where: {name: demoReload.val()},
      done:function(res){
        soulTable.render(this);
        $('#searchName').val(demoReload.val());
      }
    });
  }else{
    renderTreeTable();
  }
}
//更新前的收集items名字
var items = [
  "id", "parentdepartmentid", "name", "sort",
  "description","type","area"
];
//更新前的收集
function pickData(){
  var obj = {};
  for( var i=0; i<items.length; i++ ){
    var val = $("#"+ items[i]).val().split('||')[0];
    obj[ items[i] ] = val;
  }
  return obj;
}
//更新
function save(){
  var _data = pickData();
  console.log("Data:"+ _data );
  $.ajax({
    url:'${ctxPath}/updateBySysDepartMentPrimaryKey',
    type:'post',
    data:_data,
    dataType:'json',
    success:function( data ){
      var code = data['code'];
      layer.closeAll();
      if(code=='0'){
        layer.msg('更新'+_data['name']+'成功',{icon:1,time:2000,offset:'20px'});
        reloadPage();
      }else{
        layer.msg('更新'+_data['name'] +'失败, 原因:'+ data['msg'],{icon:2,time:2000,offset:'20px'});
      }
    }
  });
}
</script>
</body>
</html>        

